<?php
/**
 * Created by PhpStorm.
 * User: Jason
 * Date: 2017/8/5
 * Time: 19:04
 */

namespace Api\Service;

use Think\Log;
class UserMemberService
{
    /**
     * 验证用户是否会员
     */
    public function verifyMember($uid)
    {
        $member = M('UserCoinWallet')->where(array('user_id'=>$uid))->find();
        if (!empty($member)) {
            if ($member['amount'] > 0) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 获取后台会员收费设定
     */
    public function coinCharge($param)
    {
        //step-1: 获取用户帐户金币余额
        $userModel = M('UserCoinWallet');
        $userAccount = $userModel->where(array('user_id'=>$param['uid']))->find();
        $data = array();
        if ($userAccount) {
            $data['account']['user_id'] = $userAccount['user_id'];
            $data['account']['amount'] = $userAccount['amount'];
        } else {
            $data['account']['user_id'] = $param['uid'];
            $data['account']['amount'] = 0;
        }

        //step-2: 获取收费设定
        $model = M('UserCoinCharge');
        $map = array(
            'is_delete'=>0
        );
        $chargeSet = $model->where($map)->field('id, name, amount, additional_coin, original_price, discount_price')->select();
        $data['charge_set'] = $chargeSet;
        return $data;
    }

    /**
     * 充值会员帐户金币
     * @param $uid  /用户id
     * @param $amount /充值金币数量
     * @param int $style  /乐器类型
     * @return mixed
     */
    public function rechargeUserAccount($uid, $amount, $style=1)
    {
        $model = M('UserCoinWallet');
        $map = array('user_id'=>$uid);
        $user = $model->where($map)->find();
        if (!empty($user)) {
            $data = array(
                'amount' => $user['amount'] + $amount,
                'update_time' => date('Y-m-d H:i:s', time())
            );
            $res = $model->where($map)->save($data);
        } else {
            $data = array(
                'user_id' => $uid,
                'amount' => $amount,
                'style' => $style,
            );
            $res = $model->add($data);
        }
        Log::writeInfo('Recharge coin. The original coin amount is '.$user['amount'].'.And now the coin amount is '.$data['amount'], 'wxReply');
        return $res;
    }

    /**
     * 更新用户金币流水
     * @param $uid
     * @param $amount 充值为正，消费为负
     * @param $type 1充值，2消费
     * @param $time
     * @param $orderType 100-充值金币 ；200-消费会员 201-购买曲谱
     * @param $orderNo
     * @return mixed
     */
    public function updateUserPayment($uid, $amount, $type, $time, $orderType, $orderNo)
    {
        $model = M('UserWalletList');
        $data = array(
            'user_id' => $uid,
            'amount' => $amount,
            'type' => $type,
            'create_time' => $time,
            'order_type' => $orderType,
            'order_id' => $orderNo
        );
        $id = $model->add($data);
        return $id;
    }
}